package com.hzc.springchan.service.impl.business;

import com.hzc.springchan.bean.entity.UserEntity;
import com.hzc.springchan.bean.request.Request;
import com.hzc.springchan.bean.response.MapDataResponse;
import com.hzc.springchan.constant.System;
import com.hzc.springchan.enums.LogOperation;
import com.hzc.springchan.mapper.UserMapper;
import com.hzc.springchan.service.IBusinessService;
import com.hzc.springchan.service.IOperationLogService;
import com.hzc.springchan.service.ITokenService;
import com.hzc.springchan.util.LogUtils;
import com.hzc.springchan.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 修改用户信息业务处理类
 * @Author: chen990627
 * @Date: 2022/11/07
 * **/
@Service(System.ApiBusinessName.PUT_USER_INFO)
public class PutUserInfoBusiness implements IBusinessService<Request, MapDataResponse> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IOperationLogService operationLogService;

    @Autowired
    private ITokenService tokenService;

    @Override
    public void doBusiness(Request request, MapDataResponse response) {

        String nickName = (String) request.get("nickName");
        String passWord = (String) request.get("passWord");
        String token = (String) request.get("token");
        Integer userId = request.getUserId();
        Map<String, Object> findUserMap = new HashMap<>(8);
        findUserMap.put("userId", userId);
        List<UserEntity> userEntityList = userMapper.findByOption(findUserMap);
        UserEntity userEntity = userEntityList.get(0);

        try {
            String encodedPassWord = MD5Utils.encodeByMd5(passWord);
            Map<String, Object> userMap = new HashMap<>(8);
            userMap.put("userId", userId);
            userMap.put("nickName", nickName);
            userMap.put("passWord", encodedPassWord);
            userMapper.modify(userMap);

            IOperationLogService.OperationRequest operationRequest = new IOperationLogService.OperationRequest();
            operationRequest.setUserId(userId);
            operationRequest.setOperation(LogOperation.PUT_USER_INFO);
            Map<String, Object> operationMap = new HashMap<>(8);
            operationMap.put("userName", userEntity.getUserEmail());

            Date date = new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String curDate = dateFormat.format(date);
            operationMap.put("createTime", curDate);
            operationRequest.setParamMap(operationMap);
            operationLogService.logOperation(operationRequest);

            tokenService.expire(token, System.RequestParam.USER_ID);
            response.setSuccess();
        } catch (NoSuchAlgorithmException e) {
            LogUtils.info(this, "No such algorithm exception!");
        } catch (UnsupportedEncodingException e) {
            LogUtils.info(this,"Unsupported encoding exception!");
        }
    }
}
